package com.sch.www.common.base;


import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.ValidationError;
import org.springframework.http.HttpStatus;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/10
 * 版权所有: admin
 */
public class ResultBean<T> {

    //成功
    public static final String SUCCESS = "10000";
    //失败
    public static final String ERROR = "20000";

    private HttpStatus httpStatus = HttpStatus.OK;
    private String code = SUCCESS;
    private StringBuffer msg;
    private T data;


    public static <T> ResultBean<T> newInstance() {
        ResultBean resultBean = new ResultBean();
        return resultBean;
    }


    //=========================
    //       code
    //=========================

    public ResultBean<T> setSuccess() {
        return setCode(SUCCESS);
    }

    public ResultBean<T> setError() {
        return setCode(ERROR);
    }

    public String getCode() {
        return code;
    }

    public ResultBean<T> setCode(String code) {
        this.code = code;
        return this;
    }


    //=========================
    //       msg
    //=========================

    public ResultBean<T> appendMsg(String proPostSplit, String msg) {
        return appendMsg(proPostSplit, msg, proPostSplit);
    }

    public ResultBean<T> appendMsg(String proSplit, String msg, String postSplit) {
        appendMsg(proSplit);
        appendMsg(msg);
        appendMsg(postSplit);
        return this;

    }

    public ResultBean<T> appendMsg(String msg) {
        if (this.msg == null) {
            this.msg = new StringBuffer();
        }
        this.msg.append(msg);
        return this;
    }

    public ResultBean<T> appendMsg(ComplexResult complexResult) {
        for (ValidationError error : complexResult.getErrors()) {
            appendMsg("<p>", error.getErrorMsg(), "</p>");
        }
        return this;
    }

    public ResultBean<T> setMsg(String msg) {
        this.msg = new StringBuffer(msg);
        return this;
    }

    public String getMsg() {
        if (msg==null){
            return "";
        }else{
            return msg.toString();
        }

    }

    //=========================
    //       data
    //=========================
    public T getData() {
        return data;
    }

    public ResultBean<T> setData(T data) {
        this.data = data;
        return this;
    }


    //=========================
    //       httpStatus
    //=========================

    public ResultBean<T> setHttpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
        return this;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }
}
